<?php

namespace XWAM\Controller;

/**
 * Description of MenuController
 *
 * @since 1.0 <2015-9-18> SoChishun <14507247@qq.com> Added.
 */
class MenuController {

    /**
     * 获取用户菜单
     * @since 1.0 <2015-9-18> SoChishun Added.
     * @since 1.0 <2015-9-23> SoChishun 重构为Auth方法.
     */
    public function get_menu($focus = 'N') {
        header('Content-Type:application/json; charset=utf-8');
        $login_data = session(C('USER.SESSION_ID'));
        if (!$login_data) {
            exit(json_encode(array('status' => false, 'info' => '未登录')));
        }
        $auth = new \Think\AuthX();
        $menus = $auth->getMenus($login_data['id']);
        // 客户端定时刷新菜单,为避免不必要的数据加载和数据填充时界面闪烁,如果菜单md5一致，则当成加载失败处理，不刷新客户端
        if ($menus['status']) {
            $md5key = md5(serialize($menus['info']));
            $keys = F('menu_md5');
            $ukey = 'u' . $login_data['id'];
            $save_key = true;
            if (isset($keys[$ukey]) && ($md5key == $keys[$ukey])) {
                $save_key = false;
                if ('N' == $focus) {
                    $menus = array('status' => false, 'info' => '签名无变化');
                }
            }
            $menus['save'] = $save_key;
            if ($save_key) {
                $keys[$ukey] = $md5key;
                F('menu_md5', $keys);
            }
        }
        exit(json_encode($menus));
    }

}
